<?php
//后台权限管理

define('IN_MMOA', true);

require dirname(__FILE__).'/includes/init.php';
//print_r($mmoa); exit();

//echo $_REQUEST['act']; //exit();

if ($_REQUEST['act'] == '')
{
	
	$smarty->assign('mm_show', 'permisson_list');

	clear_caches();
	$smarty->display('permission.htm');
}

elseif ($_REQUEST['act'] == 'group')
{
	$sql = "SELECT * FROM " . $mmoa->table('group') . " WHERE 1";
	$group = $db->getAll($sql);
	$smarty->assign('group', $group);

	$smarty->assign('mm_show', 'permisson_group');

	clear_caches();
	$smarty->display('permission.htm');
}
elseif ($_REQUEST['act'] == 'addgroup')
{
	$sqle = "SELECT id, e_name FROM " . $mmoa->table('employees') . "where 1";
	//$sqle = "SELECT id, e_name FROM " . $mmoa->table('employees') . "where $where";
	$einfo = $db->getAll($sqle);

	$smarty->assign('einfo', $einfo);
	
	$smarty->assign('mm_show', 'addgroup');

	clear_caches();
	$smarty->display('permission.htm');
}
elseif ($_REQUEST['act'] == 'addgroupu')
{
	//
	//$gid = $_POST['gid'];
	$gname = $_POST['gname'];
	$gmember = $_POST['g_list'];
	echo $gname.' - '.$gmember; echo '  '; //exit();
	if($gname){
		$sql = "INSERT INTO " . $mmoa->table('group') . " (g_name, g_member) VALUES ('$gname', '$gmember')" ;
		if ($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo "添加成功"; exit();
		} else {
			echo '<meta charset="utf-8">';
			echo "添加失败"; exit();
		}
	}
}
elseif ($_REQUEST['act'] == 'modgroup')
{
	$gid = $_GET['gid'];
	$sql = "SELECT * FROM " . $mmoa->table('group') . " where id=$gid";
	$ginfo = $db->getRow($sql); 
	//$gmember = ;
	if (trim($ginfo['g_member']))
		$gmember = split(",", trim($ginfo['g_member']));
	$count =count($gmember);
	
	$where = '';
	for ($i=0; $i<$count-1; $i++){
		$where .= " e_name <> '" . $gmember[$i] . "' AND ";
	}
	$where .= " e_name <> '" . $gmember[$count-1] . "'";

	//$sqle = "SELECT id, e_name FROM " . $mmoa->table('employees') . "where 1";
	$sqle = "SELECT id, e_name FROM " . $mmoa->table('employees') . "where $where";
	$einfo = $db->getAll($sqle);
	
	
	$smarty->assign('gmember', $gmember);
	$smarty->assign('ginfo', $ginfo);

	$smarty->assign('einfo', $einfo);
	
	$smarty->assign('mm_show', 'modgroup');

	clear_caches();
	$smarty->display('permission.htm');
}
elseif ($_REQUEST['act'] == 'modgroupu')
{
	//
	$gid = $_POST['gid'];
	$gname = $_POST['gname'];
	$gmember = $_POST['g_list'];
	echo $gmember; echo '  '; //exit();
	if($gid){
		$sql = "UPDATE " . $mmoa->table('group') . " SET g_name = '$gname', g_member='$gmember' WHERE id=$gid" ;
		if ($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo "修改成功"; exit();
		} else {
			echo '<meta charset="utf-8">';
			echo "修改失败"; exit();
		}
	}
}


elseif ($_REQUEST['act'] == 'delgroup')
{
	$gid = $_GET['gid'];
	if ($gid){
		$sql = "DELETE FROM " . $mmoa->table('group') . " WHERE id=$gid" ;
		if ($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo "删除成功"; exit();
		} else {
			echo '<meta charset="utf-8">';
			echo "删除失败"; exit();
		}
		
	}
	//$smarty->assign('mm_show', 'delgroup');

	clear_caches();
	//$smarty->display('permission.htm');
}

elseif ($_REQUEST['act'] == 'allocation')
{
	$sql = "SELECT * FROM " . $mmoa->table('modules') . "where 1";
	$minfo = $db->getAll($sql);
	
	$smarty->assign('minfo', $minfo);

	$smarty->assign('mm_show', 'permisson_allocation');

	clear_caches();
	$smarty->display('permission.htm');
}
elseif ($_REQUEST['act'] == 'modallocation')
{
	$mid = $_GET['mid'];
	$sql = "SELECT * FROM " . $mmoa->table('modules') . " where id=$mid";
	$minfo = $db->getRow($sql);   //选择修改的功能模块
	 //print_r($minfo);
	if (trim($minfo['m_group']))
		$mgroup = split(",", trim($minfo['m_group']));
	$count =count($mgroup);   //功能模块所属组
	
	$where = '';
	for ($i=0; $i<$count-1; $i++){
		$where .= " g_name <> '" . $mgroup[$i] . "' AND ";
	}
	$where .= " g_name <> '" . $mgroup[$count-1] . "'";
	
	$sqlg = "SELECT id, g_name FROM " . $mmoa->table('group') . "where $where";
	$ginfo = $db->getAll($sqlg);  //组信息
	
	$smarty->assign('mgroup', $mgroup);
	$smarty->assign('minfo', $minfo);
	
	$smarty->assign('ginfo', $ginfo);
	
	$smarty->assign('mm_show', 'modallocation');

	clear_caches();
	$smarty->display('permission.htm');
}
elseif ($_REQUEST['act'] == 'modallocationu')
{
	//
	$mid = $_POST['mid'];
	//$gname = $_POST['gname'];
	$mgroup = $_POST['g_list'];
	echo $mgroup; echo '  '; //exit();
	if($mid){
		$sql = "UPDATE " . $mmoa->table('modules') . " SET m_group = '$mgroup' WHERE id=$mid" ;
		if ($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo "修改成功"; exit();
		} else {
			echo '<meta charset="utf-8">';
			echo "修改失败"; exit();
		}
	}
	//$smarty->assign('mm_show', 'modallocation');

	clear_caches();
	//$smarty->display('permission.htm');
}
?>